﻿using System.Modules.ExceptionHanlding;
using Microsoft.BusinessData.MetadataModel;
using Microsoft.SharePoint.BusinessData.MetadataModel;

namespace System.Modules.SharePoint.Extensions
{
    /// <summary>
    /// Business Data Connectivity Extensions
    /// </summary>
    public static class BusinessDataConnectivityExtensions
    {
        #region Try Get Entity
        /// <summary>
        /// This Method Tries to get Entity
        /// </summary>
        /// <param name="catalog"></param>
        /// <param name="nameSpace"></param>
        /// <param name="entityName"></param>
        /// <returns>IEntity</returns>
        public static IEntity TryGetEntity(this DatabaseBackedMetadataCatalog catalog, string nameSpace, string entityName)
        {
            IEntity entity = null;
            try
            {
                entity = catalog.GetEntity(nameSpace, entityName);
            }
            catch (Exception exception)
            {
                exception.Handle();
            }
            return entity;
        }
        #endregion

        #region Try Get Method Instance
        /// <summary>
        /// This Method Tries to get Method Instance
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="methodName"></param>
        /// <param name="methodInstanceType"></param>
        /// <returns>IMethodInstance</returns>
        public static IMethodInstance TryGetMethodInstance(this IEntity entity, string methodName, MethodInstanceType methodInstanceType)
        {
            IMethodInstance methodInstance = null;
            try
            {
                methodInstance = entity.GetMethodInstance(methodName, methodInstanceType);
            }
            catch (Exception exception)
            {
                exception.Handle();
            }
            return methodInstance;
        }
        #endregion
    }
}